package com.hoshiicloud.market.controller.market;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.goods.entity.category.SalesCategory;
import com.hoshiicloud.market.param.category.SalesCategoryQueryParam;
import com.hoshiicloud.market.param.category.SalesCategoryUpdateParam;
import com.hoshiicloud.market.param.category.SalesCategoryAddParam;
import com.hoshiicloud.market.service.market.SalesCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiicloud.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 营销类目 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-17
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/sales-category")
@Api(value = "商品管理", tags = {"营销类目管理"})
public class SalesCategoryController {
    @Autowired
    private SalesCategoryService salesCategoryService;

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 分页查询营销类目
    */
    @ApiOperation(value = "分页查询营销类目")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid SalesCategoryQueryParam param) {
        try {
            log.info("分页查询营销类目");
            QueryWrapper<SalesCategory> queryWrapper = new QueryWrapper<SalesCategory>();
            queryWrapper.eq(param.getCompanyid() != null, "companyid", param.getCompanyid());
            queryWrapper.eq(param.getParentId() != null, "parent_id", param.getParentId());
            queryWrapper.eq(StringUtils.isNotBlank(param.getName()), "name", param.getName());
            queryWrapper.eq(param.getWeight() != null, "weight", param.getWeight());
            queryWrapper.eq(param.getLevel() != null, "level", param.getLevel());
            queryWrapper.eq(param.getOrdered() != null, "ordered", param.getOrdered());
            queryWrapper.eq(StringUtils.isNotBlank(param.getImages()), "images", param.getImages());
            queryWrapper.eq(StringUtils.isNotBlank(param.getRemark()), "remark", param.getRemark());
            queryWrapper.eq(param.getIsLeaf() != null, "is_leaf", param.getIsLeaf());
            queryWrapper.eq(param.getDeleted() != null, "deleted", param.getDeleted());
            IPage<SalesCategory> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(SalesCategory.class));
            return Response.data(salesCategoryService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询营销类目异常", e);
            return Response.failed("分页查询营销类目异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 根据id查询营销类目
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询营销类目")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(salesCategoryService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增营销类目")
    public Response save(@RequestBody @Valid SalesCategoryAddParam param) {
        try {
            SalesCategory data = BeanUtils.covertType(param, SalesCategory.class);
            return Response.data(salesCategoryService.save(data));
        } catch (ServiceException e) {
            log.error("新增营销类目异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增营销类目异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiicloud.common.rpc.Response
    * @description: 修改营销类目
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改营销类目")
    public Response update(@RequestBody @Valid SalesCategoryUpdateParam param) {
        try {
            SalesCategory data = BeanUtils.covertType(param, SalesCategory.class);
            return Response.data(salesCategoryService.updateById(data));
        }catch (ServiceException e) {
            log.error("修改营销类目异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改营销类目异常", e);
            return Response.failed();
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除营销类目
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除营销类目")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        try {
            SalesCategory data=salesCategoryService.getById(id);
            if(data==null){
                log.error("删除营销类目异常，无法找到营销类目记录");
                return Response.failed("删除营销类目异常，无法找到营销类目记录");
            }
            salesCategoryService.deleteById(data);
            return Response.success("删除营销类目成功");
        }catch (ServiceException e) {
            log.error("删除营销类目异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("删除营销类目异常", e);
            return Response.failed();
        }
    }

    /**
    * @Author:campchen
    * @Description: StringUtils.join(ids, "") 为缓存中添加唯一KEY  如不合适可选择更好方法
    * @Date:上午 10:18 2019/5/11
    * @Param:  ids
    * @return: com.hoshiicloud.common.rpc.Response
    */
    @ApiOperation(value = "根据id集合查询营销类目")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if(CollectionUtils.isNotEmpty(ids)){
            ids.sort(null);
            return Response.data(salesCategoryService.getByIds(ids,StringUtils.join(ids, "")));
        }else{
            return  Response.data(null);
        }
    }


    }
